var carr = {
    Car_Image_Width : null,
    Car_Image_Height : null,
    Car_Border : null,
    Car_Border_Color : null,
    Car_Speed : null,
    Car_Direction : null,
    Car_NoOfSides : null,
    Car_Image_Sources : null,
    CW_I : null,
    C_ClcW : null,
    C_Coef : null,
    C_CoefOf : null,
    C_Pre_Img : null,
    C_Angle : null,
    C_CrImg : null,
    Car_NoOfSides : null,
    C_MaxW : null,
    C_TotalW : null,
    C_Stppd : null,
    C_LeftOffset : null,
    C_HalfNo : null,
    
    getLink : function(str) {
        var re = /([\w]+)\.jpg/;
        var m = re.exec(str);
        var s = "";

        if (m == null) {
            return "";
        } else {
            return m[1];
        }
    },
    
    titleCase : function(str) {
        var strOut = "";
        while ((/(\b\w)(\S+\s*)/g.exec(str)) != null)
            strOut += RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase();
    
        return strOut;
    },
    
    init : function() {
        carr.Car_Image_Width = 224;
        carr.Car_Image_Height = 152;
        carr.Car_Border = true;
        carr.Car_Border_Color = "white";
        carr.Car_Speed = 2;
        carr.Car_Direction = true;
        carr.Car_NoOfSides = 12;
        carr.Car_Image_Sources = new Array( "../img/carr/aulas1.jpg", "aulas1", 
                                            "../img/carr/aulas2.jpg", "aulas2", 
                                            "../img/carr/aulas3.jpg", "aulas3", 
                                            "../img/carr/aulas4.jpg", "aulas4", 
                                            "../img/carr/biblio.jpg", "biblio", 
                                            "../img/carr/estacionamiento.jpg", "estacionamiento", 
                                            "../img/carr/oficinas1.jpg", "oficinas1", 
                                            "../img/carr/oficinas3.jpg", "oficinas3", 
                                            "../img/carr/oficinas5.jpg", "oficinas5");
        
        carr.CW_I = new Array(carr.Car_NoOfSides / 2 + 1);
        carr.C_ClcW = new Array(carr.Car_NoOfSides / 2);
        carr.C_Coef = new Array(3 * Math.PI / 2, 
                                0, 
                                3 * Math.PI / 2, 
                                11 * Math.PI / 6, 
                                Math.PI / 6, 
                                3 * Math.PI / 2, 
                                7 * Math.PI / 4, 
                                0, Math.PI / 4, 
                                3 * Math.PI / 2, 
                                5 * Math.PI / 3, 
                                11 * Math.PI / 6, 
                                0, 
                                Math.PI / 6, 
                                Math.PI / 3);
        carr.C_CoefOf = carr.Car_NoOfSides == 4 ? 0 : carr.Car_NoOfSides == 6 ? 2 : carr.Car_NoOfSides == 8 ? 5 : 9;
        carr.C_Pre_Img = new Array(carr.Car_Image_Sources.length);
        carr.C_Angle = carr.Car_Direction ? Math.PI / (carr.Car_NoOfSides / 2) : 0;
        carr.C_CrImg = carr.Car_NoOfSides;
        carr.C_MaxW = 0;
        carr.C_TotalW = 0;
        carr.C_Stppd = false; 
        carr.C_LeftOffset = 0;
        carr.C_HalfNo = carr.Car_NoOfSides / 2;
        
        carr.start();
    },
    
    start : function() {
        if (document.getElementById) {
            for (var i = 0; i < carr.Car_Image_Sources.length; i += 2) {
                carr.C_Pre_Img[i] = new Image();
                carr.C_Pre_Img[i].src = carr.Car_Image_Sources[i];
            }
            
            carr.C_MaxW = carr.Car_Image_Width / Math.sin(Math.PI / carr.Car_NoOfSides) + carr.C_HalfNo + 1;
            carr.Car_Div = document.getElementById("Carousel");
            
            if (!carr.Car_Div) { return; }
            
            for (var i = 0; i < carr.C_HalfNo; i++) {
                carr.CW_I[i] = document.createElement("img");
                carr.CW_I[i].className = "lbOn";
                carr.CW_I[i].rel = carr.Car_Image_Sources[2 * i + 1];
                carr.Car_Div.appendChild(carr.CW_I[i]);
                carr.CW_I[i].style.position = "absolute";
                carr.CW_I[i].style.top = "0px";
                carr.CW_I[i].style.height = carr.Car_Image_Height + "px";
                
                if (carr.Car_Border) {
                    carr.CW_I[i].style.borderStyle = "solid";
                    carr.CW_I[i].style.borderWidth = "1px";
                    carr.CW_I[i].style.borderColor = carr.Car_Border_Color;
                }
                
                carr.CW_I[i].src = carr.Car_Image_Sources[2 * i];
                carr.CW_I[i].lnk = carr.getLink(carr.Car_Image_Sources[2 * i]); //carr.Car_Image_Sources[2 * i + 1];
                carr.CW_I[i].title = carr.titleCase(carr.getLink(carr.Car_Image_Sources[2 * i])); //carr.Car_Image_Sources[2 * i];
                carr.CW_I[i].onclick = carr.C_LdLnk;
                carr.CW_I[i].onmouseover = carr.C_Stp;
                carr.CW_I[i].onmouseout = carr.C_Rstrt;
                
                //alert(carr.CW_I[i].src + " -- " + carr.CW_I[i].title);
            }
            
            carr.CarImages();
        }
    },
    
    CarImages : function() {
        if (!carr.C_Stppd) {
            carr.C_TotalW = 0;
            
            for (var i = 0; i < carr.C_HalfNo; i++) {
                carr.C_ClcW[i] = Math.round(Math.cos(Math.abs(carr.C_Coef[carr.C_CoefOf + i] + carr.C_Angle)) * carr.Car_Image_Width);
                carr.C_TotalW += carr.C_ClcW[i];
            }
            
            carr.C_LeftOffset = (carr.C_MaxW - carr.C_TotalW) / 2;
            
            for (var i = 0; i < carr.C_HalfNo; i++) {
                carr.CW_I[i].style.left = carr.C_LeftOffset + "px";
                carr.CW_I[i].style.width = carr.C_ClcW[i] + "px";
                carr.C_LeftOffset += carr.C_ClcW[i];
            }
            
            carr.C_Angle += carr.Car_Speed / 720 * Math.PI * (carr.Car_Direction ? -1 : 1);
            
            if (carr.Car_Direction && carr.C_Angle <= 0 ||
                !carr.Car_Direction && carr.C_Angle >= Math.PI / carr.C_HalfNo) {
                if (carr.C_CrImg == carr.Car_Image_Sources.length) {
                    carr.C_CrImg = 0;
                }

                if (carr.Car_Direction) {
                    carr.CW_I[carr.C_HalfNo] = carr.CW_I[0];

                    for (var i = 0; i < carr.C_HalfNo; i++) {
                        carr.CW_I[i] = carr.CW_I[i + 1];
                    }

                    if (carr.C_HalfNo >= 1) {
                        carr.CW_I[carr.C_HalfNo - 1].src = carr.Car_Image_Sources[carr.C_CrImg];
                        carr.CW_I[carr.C_HalfNo - 1].lnk = carr.getLink(carr.Car_Image_Sources[carr.C_CrImg]); //carr.Car_Image_Sources[carr.C_CrImg + 1];
                        carr.CW_I[carr.C_HalfNo - 1].rel = carr.getLink(carr.Car_Image_Sources[carr.C_CrImg]); //carr.Car_Image_Sources[carr.C_CrImg + 1];

                        carr.CW_I[carr.C_HalfNo - 1].title = carr.titleCase(carr.getLink(carr.Car_Image_Sources[carr.C_CrImg])); //carr.Car_Image_Sources[carr.C_CrImg + 1];
                        
                        Event.unloadCache();
                        initialize();
                    }
                } else {
                    for (var i = carr.C_HalfNo; i > 0; i--) {
                        carr.CW_I[i] = carr.CW_I[i - 1];
                    }

                    carr.CW_I[0] = carr.CW_I[carr.C_HalfNo];
                    carr.CW_I[0].src = carr.Car_Image_Sources[carr.C_CrImg];
                    carr.CW_I[0].lnk = carr.getLink(carr.Car_Image_Sources[carr.C_CrImg]); //carr.Car_Image_Sources[carr.C_CrImg + 1];
                    carr.CW_I[0].rel = carr.getLink(carr.Car_Image_Sources[carr.C_CrImg]); //carr.Car_Image_Sources[carr.C_CrImg + 1];

                    carr.CW_I[0].title = carr.titleCase(carr.getLink(carr.Car_Image_Sources[carr.C_CrImg])); //carr.Car_Image_Sources[carr.C_CrImg + 1];
                    
                    //alert("@@@" + carr.CW_I[0].lnk + " --- " + carr.CW_I[0].title)
                }

                carr.C_Angle = carr.Car_Direction ? Math.PI / carr.C_HalfNo : 0;
                carr.C_CrImg += 2;
            }
        }

        setTimeout(function() { carr.CarImages(); }, 50);
    },


    C_LdLnk : function() {
        if (this.lnk) {
            window.location.href = "#" + carr.getLink(this.src);
        }
    },


    C_Stp : function() {
        this.style.cursor = this.lnk ? "pointer" : "default";
        carr.C_Stppd = true;
    },


    C_Rstrt : function() {
        carr.C_Stppd = false;
    }
};